/*
 * A virtual bus for LDD sample code devices to plug into.  This
 * code is heavily borrowed from drivers/base/sys.c
 *
 * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
 * Copyright (C) 2001 O'Reilly & Associates
 *
 * The source code in this file can be freely used, adapted,
 * and redistributed in source or binary form, so long as an
 * acknowledgment appears in derived source files.  The citation
 * should list that the code comes from the book "Linux Device
 * Drivers" by Alessandro Rubini and Jonathan Corbet, published
 * by O'Reilly & Associates.   No warranty is attached;
 * we cannot take responsibility for errors or fitness for use.
 *
 */
/* $Id: lddbus.c,v 1.9 2004/09/26 08:12:27 gregkh Exp $ */

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include "lddbus.h"

MODULE_AUTHOR("Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");
static char *Version = "$Revision: 1.9 $";

/*
 * Respond to hotplug events.
 */
static int ldd_hotplug(struct device *dev, struct kobj_uevent_env *env)
{
	printk(KERN_WARNING "ldd_hotplug feng \n");
	//dump_stack();

	if(add_uevent_var(env, "LDDBUS_VERSION=%s",
				Version)) {
		return -ENOMEM;
	}

	if(add_uevent_var(env, "LIUFENG_UEVENT_VERSION=%s",
				Version)) {
		return -ENOMEM;
	}

	return 0;
}

/*
 * Match LDD devices to drivers.  Just do a simple name test.
 */
static int ldd_match(struct device *dev, struct device_driver *driver)
{
	int n = ~0;
	printk(KERN_WARNING "ldd_match: dev_name(dev) = %s\n", dev_name(dev));
	printk(KERN_WARNING "ldd_match: driver->name = %s\n", driver->name);

	n = strncmp(dev_name(dev), driver->name, strlen(driver->name));
	printk(KERN_WARNING "ldd_match: strncmp result= %d\n", n);

	return !strncmp(dev_name(dev), driver->name, strlen(driver->name));
}


/*
 * The LDD bus device.
 */
static void ldd_bus_release(struct device *dev)
{
	printk(KERN_DEBUG "lddbus release\n");
}

/*
 * And the bus type.
 */
struct bus_type ldd_bus_type = {
	.name = "ldd_bus_type_feng",
	.match = ldd_match,
	.uevent  = ldd_hotplug,
};

struct device ldd_bus = {
	.init_name   = "ldd0_bus_root_device_feng",
	//.bus = &ldd_bus_type,
	.release  = ldd_bus_release
};


/*
 * Export a simple attribute.
 */
static ssize_t show_bus_version(struct bus_type *bus, char *buf)
{
	int n = 0;
	n += snprintf(buf + n, PAGE_SIZE, "%s\n", Version);
	n += snprintf(buf + n, PAGE_SIZE, "liufeng %s\n", Version);
	return n;
}

static BUS_ATTR(version_feng, S_IRUGO, show_bus_version, NULL);



/*
 * LDD devices.
 */

/*
 * For now, no references to LDDbus devices go out which are not
 * tracked via the module reference count, so we use a no-op
 * release function.
 */
static void ldd_dev_release(struct device *dev)
{ 
	printk(KERN_WARNING "ldd_dev_release feng \n");
	return;
}

int register_ldd_device(struct ldd_device *ldddev)
{
	ldddev->dev.bus = &ldd_bus_type;
	ldddev->dev.parent = &ldd_bus;
	ldddev->dev.release = ldd_dev_release;
	printk(KERN_WARNING "register_ldd_device: before dev_set_name ldddev->dev.kobj.name = %s, ldddev->name = %s\n", ldddev->dev.kobj.name, ldddev->name);
	dev_set_name(&ldddev->dev, "%s", ldddev->name);
	printk(KERN_WARNING "register_ldd_device: after dev_set_name ldddev->dev.kobj.name = %s, ldddev->name = %s\n", ldddev->dev.kobj.name, ldddev->name);

	return device_register(&ldddev->dev);
}
EXPORT_SYMBOL(register_ldd_device);

void unregister_ldd_device(struct ldd_device *ldddev)
{
	device_unregister(&ldddev->dev);
}
EXPORT_SYMBOL(unregister_ldd_device);

/*
 * Crude driver interface.
 */


static ssize_t show_version(struct device_driver *driver, char *buf)
{
	int n = ~0;
	struct ldd_driver *ldriver = to_ldd_driver(driver);

	n += sprintf(buf + n, "%s\n", ldriver->version);
	n += sprintf(buf + n, "feng show ldriver->version %s\n", ldriver->version);

	return strlen(buf);
}
		

int register_ldd_driver(struct ldd_driver *driver)
{
	int ret;
	
	printk(KERN_WARNING "register_ldd_driver feng\n");

	driver->driver.bus = &ldd_bus_type;
	ret = driver_register(&driver->driver);
	if (ret)
		return ret;
	driver->version_attr.attr.name = "version_ldd_driver_feng";
	driver->version_attr.attr.mode = S_IRUGO;
	driver->version_attr.show = show_version;
	driver->version_attr.store = NULL;
	return driver_create_file(&driver->driver, &driver->version_attr);
}

void unregister_ldd_driver(struct ldd_driver *driver)
{
	driver_unregister(&driver->driver);
}
EXPORT_SYMBOL(register_ldd_driver);
EXPORT_SYMBOL(unregister_ldd_driver);



static int __init ldd_bus_init(void)
{
	int ret;

	ret = bus_register(&ldd_bus_type);
	if (ret)
		return ret;
	if (bus_create_file(&ldd_bus_type, &bus_attr_version_feng))
		printk(KERN_NOTICE "Unable to create version attribute\n");
	ret = device_register(&ldd_bus);
	if (ret)
		printk(KERN_NOTICE "Unable to register ldd0\n");
	printk(KERN_WARNING "register bus_type, bus_attre_version, ldd_bus device done\n");		
	return ret;
}

static void ldd_bus_exit(void)
{
	device_unregister(&ldd_bus);
	bus_unregister(&ldd_bus_type);
}

module_init(ldd_bus_init);
module_exit(ldd_bus_exit);
